package com.ww.springboot.dd.dto;

import org.springframework.lang.Nullable;

import java.util.HashMap;
import java.util.Map;

public class ThreadLocalUtil {
    private static final ThreadLocal<Map<String, Object>> LOCAL = ThreadLocal.withInitial(HashMap::new);

    public ThreadLocalUtil() {
    }

    public static Map<String, Object> getAll() {
        return new HashMap((Map) LOCAL.get());
    }

    public static <T> T put(String key, T value) {
        ((Map) LOCAL.get()).put(key, value);
        return value;
    }

    public static void put(Map<String, Object> map) {
        ((Map) LOCAL.get()).putAll(map);
    }

    public static void remove(String key) {
        ((Map) LOCAL.get()).remove(key);
    }

    public static void clear() {
        LOCAL.remove();
    }

    @Nullable
    public static <T> T get(String key) {
        return (T) ((Map) LOCAL.get()).get(key);
    }

    public static <T> T getAndRemove(String key) {
        Object var1;
        try {
            var1 = get(key);
        } finally {
            remove(key);
        }

        return (T) var1;
    }
}